/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Configuration;
using System.Globalization;
using System.Runtime.Serialization;

namespace DaveSexton.DocProject
{
	[Serializable]
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2240:ImplementISerializableCorrectly")]
	public class ProviderSettingsDictionary : BufferedSettingsDictionaryBase<string, string>
	{
		#region Public Properties
		public override bool CanCommit
		{
			get
			{
				return true;
			}
		}

		public ProviderSettings CommitSettings
		{
			get
			{
				return commitSettings;
			}
		}
		#endregion

		#region Private / Protected
		private readonly ProviderSettings commitSettings;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1304:SpecifyCultureInfo", MessageId = "DaveSexton.DocProject.BufferedSettingsDictionaryBase`2<System.String,System.String>.#ctor")]
		public ProviderSettingsDictionary(ProviderSettings commitSettings)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		public ProviderSettingsDictionary(ProviderSettings commitSettings, CultureInfo culture)
			: base(culture)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1304:SpecifyCultureInfo", MessageId = "DaveSexton.DocProject.BufferedSettingsDictionaryBase`2<System.String,System.String>.#ctor")]
		public ProviderSettingsDictionary(ProviderSettings commitSettings, int capacity)
			: base(capacity)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1304:SpecifyCultureInfo", MessageId = "DaveSexton.DocProject.BufferedSettingsDictionaryBase`2<System.String,System.String>.#ctor")]
		public ProviderSettingsDictionary(ProviderSettings commitSettings, IEqualityComparer<string> comparer)
			: base(comparer)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		public ProviderSettingsDictionary(ProviderSettings commitSettings, CultureInfo culture, int capacity)
			: base(culture, capacity)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1304:SpecifyCultureInfo", MessageId = "DaveSexton.DocProject.BufferedSettingsDictionaryBase`2<System.String,System.String>.#ctor")]
		public ProviderSettingsDictionary(ProviderSettings commitSettings, int capacity, IEqualityComparer<string> comparer)
			: base(capacity, comparer)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		public ProviderSettingsDictionary(ProviderSettings commitSettings, CultureInfo culture, IEqualityComparer<string> comparer)
			: base(culture, comparer)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class.
		/// </summary>
		public ProviderSettingsDictionary(ProviderSettings commitSettings, CultureInfo culture, int capacity, IEqualityComparer<string> comparer)
			: base(culture, capacity, comparer)
		{
			if (commitSettings == null)
				throw new ArgumentNullException("commitSettings");

			this.commitSettings = commitSettings;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProviderSettingsDictionary" /> class for deserialization.
		/// </summary>
		protected ProviderSettingsDictionary(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}
		#endregion

		#region Methods
		public override string GetCommittedValue(string name)
		{
			if (name == null)
				throw new ArgumentNullException("name");

			return commitSettings.Parameters.Get(name);
		}

		public override void SetCommittedValue(string name, string value)
		{
			if (name == null)
				throw new ArgumentNullException("name");

			commitSettings.Parameters[name] = value;
		}

		public override bool IsDirty(string name)
		{
			if (name == null)
				throw new ArgumentNullException("name");

			if (!ContainsKey(name))
				return false;
			else
				return !string.Equals(this[name], commitSettings.Parameters.Get(name), StringComparison.CurrentCultureIgnoreCase);
		}

		public override bool IsCommitted(string name)
		{
			if (name == null)
				throw new ArgumentNullException("name");

			return commitSettings.Parameters.Get(name) != null;
		}

		public override void Delete(string name)
		{
			if (name == null)
				throw new ArgumentNullException("name");

			commitSettings.Parameters.Remove(name);
		}
		#endregion
	}
}
